package yuzhou;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;

import javax.annotation.processing.SupportedSourceVersion;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        System.setProperty("jna.encoding", "GBK");
        Kernel32.INSTANCE.SetConsoleTitleA("宇宙遨游");
        Integer hwnd = User32.INSTANCE.FindWindowA("Valve001",null);
        System.out.println("HWND:"+hwnd);
        if (hwnd==0)return;
        Pointer pid = new Memory(4);
        User32.INSTANCE.GetWindowThreadProcessId(hwnd,pid);
        System.out.println("PID:"+pid.getInt(0));
        Integer hProcess = Kernel32.INSTANCE.OpenProcess(2035711,0,pid.getInt(0));
        System.out.println("hProcess:"+hProcess);

        LPMODULEENTRY32 lp = null;
        for (LPMODULEENTRY32 i : Mem.getModule(pid.getInt(0))){
            if("client.dll".equals(i.szname)){
                lp=i;
                break;
            }
        }
        System.out.println(lp);


        String hex = "BB ?? ?? ?? ?? 83 FF 01 0F 8C ?? ?? ?? ?? 3B F8";
        String hex2 = "A1 ?? ?? ?? ?? A8 01 75 4B";
        String hex3 = "8D 34 85 ?? ?? ?? ?? 89 15 ?? ?? ?? ?? 8B 41 08 8B 48 04 83 F9 FF";


        System.out.println("hex:"+hex);

        Integer address = lp.base;
        Integer qureyLen = lp.base+lp.bsize;

        List<Integer> list = Mem.findMem(hProcess,address,qureyLen,hex);
        List<Integer> list2 = Mem.findMem(hProcess,address,qureyLen,hex2);
        List<Integer> list3 = Mem.findMem(hProcess,address,qureyLen,hex3);

        try {
            Integer addr = list.get(0);
            Integer addr2 = list2.get(0);
            Integer addr3 = list3.get(0);

            System.out.println("dwEntityList:"+Integer.toHexString(Mem.readMem(hProcess,addr+1).getInt(0)));
            System.out.println("dwGlowObjectManager:"+Integer.toHexString(Mem.readMem(hProcess,addr2+1).getInt(0)+4));
            System.out.println("dwLocalPlayer:"+Integer.toHexString(Mem.readMem(hProcess,addr3+3).getInt(0)+4));


        }catch (Exception e){
            System.err.println(e.getMessage());
        }

        Kernel32.INSTANCE.CloseHandle(hProcess);
    }
}
